#!/usr/bin/perl -w

use strict;

my $f_nPort=2004;
my $f_szServer="10.0.3.108";

# see: http://graphite.readthedocs.org/en/latest/feeding-carbon.html


# Start by simply monitoring mem.
# TODO monitor CPU usage.

# Get hostname
my $f_szHostname=`hostname`;
chomp($f_szHostname);

# Get epoch timestamp.
my $nEpochTimestamp=time();

# Get data from vmstat.
my @arVmstatDump=`vmstat -s`;

#       3788092 K total memory
#      3331752 K used memory
#      1502108 K active memory
#      1491148 K inactive memory
#       456340 K free memory
#       299668 K buffer memory

my $nActiveMemory;
my $nFreeMemory;

foreach my $szLine ( @arVmstatDump ) {
  chomp($szLine);
  if ( $szLine =~ /used memory/  ) {
    $nActiveMemory=(split('\s+', $szLine))[1];
    print "$szLine -> $nActiveMemory\n";
  } 
  if ( $szLine =~ /free memory/  ) {
    $nFreeMemory=(split('\s+', $szLine))[1];
  } 
}


# [(path, (timestamp, value)), ...]
my $szActiveMemoryData = "(${f_szHostname}.memory.active, ($nEpochTimestamp, $nActiveMemory))";
my $szFreeMemoryData = "(${f_szHostname}.memory.free, ($nEpochTimestamp, $nFreeMemory))";

my $szFullData = "[${szActiveMemoryData}, $szFreeMemoryData]";

print "$szFullData\n";
my @arOutput = `echo "$szFullData" | nc $f_szServer $f_nPort;`;
